package com.tang.nacos.controller;

import com.tang.nacos.dto.UserDto;
import com.tang.nacos.dto.UserResp;
import com.tang.nacos.service.UserService;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import javax.annotation.PostConstruct;
import javax.annotation.Resource;
import javax.validation.Valid;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.cloud.context.config.annotation.RefreshScope;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

/**
 * @Author tanglingyu
 * @Date: 2021/03/08/ 16:07
 * @Description
 */
@RefreshScope
@RestController
@RequestMapping("/nacos")
public class NacosController {

  @Value("${spring.application.name}")
  private String ss;

  @Value("${tly.name}")
  private String name;

  @PostConstruct
  public void a(){
    System.out.println(ss);
    System.out.println(name);
  }

  @Resource
  private UserService userService;


  @RequestMapping(value = {"/mysql1", "/mysql2"})
  public List<UserDto> findAll() {
    return userService.findAllUsers();
  }

  @RequestMapping("/test")
  public List<UserResp> getUserListByRoleId() {
    return userService.getUserListByRoleId(1L);
  }

  @GetMapping("${tly.name}")
  public String placeHolder() {
    return "placeHolder";
  }

  @PostMapping("/save")
  public void saveUser(@RequestBody UserDto userDto) {
    userService.saveUser(userDto);
  }

  @PostMapping("/save2")
  public void saveUser2() {
    List<HashMap<String, String>> list = new ArrayList<>();
    HashMap map1 = new HashMap();
    map1.put("name", "tang");
    map1.put("location", "bei");
    HashMap map2 = new HashMap();
    map2.put("name", "ling");
    map2.put("location", "xian");
    HashMap map3 = new HashMap();
    map3.put("name", "sb");
    map3.put("location", "sb");
    list.add(map1);
    list.add(map2);
    list.add(map3);
    userService.saveUser2(list);
  }
}

